********************************************
* TABLE 4: CLASS X SCORES
********************************************
preserve 

gen classx_ext = !missing(ssc_final)
la var classx_ext "Any Exam?"
gen sample = ssc_count == 6

foreach var of varlist ssc_* {
	*standardizing the distribution
	qui summ `var' if treatment == 0
	gen z`var' = (`var' - r(mean))/r(sd)
	gen pc_`var' = `var' 
}
replace pc_ssc_final = ssc_final/500 *100

*-------------------------
*PANEL A: Without any controls
*-------------------------
est clear
foreach var of varlist classx_ext $xthmarks {

	noi di "`var' without controls"
	*1) Without any controls
	*local var "time_study_out"

	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg `var' treatment, vce(robust)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	qui eststo panela_`var': bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reg `var' treatment, vce(robust)
	*eststo: reg `var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `var' if treatment == 0
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)
	
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp
}


*----------------------------------------
*PANEL B: With individual and school controls
*----------------------------------------
foreach var of varlist classx_ext $xthmarks {

	noi di "`var' with controls"
	*1) Without any controls
*	local var "readcode"
	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg `var' treatment $baseoutcomes $indcontrols $schcontrols, vce(robust)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panelb_`var': qui bootstrap, seed(2905) cl(id_school) reps($reps) nodots: ///
	qui reg `var' treatment $baseoutcomes $indcontrols $schcontrols, vce(robust)

*	eststo: reg `var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `var' if treatment == 0 
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "Yes", replace
	qui estadd local sfe = "Yes", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)
	
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp
}


# delimit ;
esttab  panela* using "$tables/Table4.tex", replace
keep(treatment)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Any Exam?" "Tamil" "English" "Maths" "Science" "Social Science" "Total", pattern(1 1 1 1 1 1 1) ///
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
nomtitles
posthead("\midrule \multicolumn{8}{l}{\textit{\textbf{Panel A. Without individual and school controls}}} \\ \midrule")
postfoot("")
stats(pvalreg pvalri rsq, labels("p-val (OLS)" "p-val (RI)" "\midrule R2") ///
fmt(2 2 2)) ///
collabels(none) label booktabs nonotes
;

# delimit ;
esttab  panelb* using "$tables/Table4.tex", append
keep(treatment) ///
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par)) /// 
starlevels(* .10 ** .05 *** .01) ///
prehead("") ///
posthead("\midrule \multicolumn{8}{l}{\textit{\textbf{Panel B. With individual and school controls}}} \\ \midrule")
stats(pvalreg pvalri mean rsq N, labels("p-val (OLS)" "p-val (RI)" "\midrule Control mean" "\midrule R2" "N") ///
fmt(2 2 2 2 0)) ///
collabels(none) mlabels(none) nonumbers label booktabs nonotes nomtitles;
#delimit cr	






********************************************
* TABLE 5: CLASS X SCORES FOR WEAK STUDENTS
********************************************
cap drop weak*
gen weak = math<=4
gen weakXtreat = weak * treatment 
la var weak "Weak"
la var weakXtreat "Treat x Weak"

*-------------------------
*PANEL A: Without any controls
*-------------------------

* Set 1: Standardized Test Scores
est clear
foreach var of varlist pc_ssc_english pc_ssc_tamil pc_ssc_math pc_ssc_final {

	noi di "`var' for Weak"
	*1) Without any controls
	*local var "time_study_out"

	qui ritest weakXtreat _b[weakXtreat], reps($reps): ///
	reg `var' treatment weakXtreat weak $indcontrols $schcontrols, vce(robust)
	matrix pvalues = r(p)
	gen temp2 = pvalues[1,1]

	qui eststo: bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reg `var' weak treatment weakXtreat $indcontrols $schcontrols, vce(robust)
	*eststo: reg `var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui test weakXtreat 
	qui estadd scalar pvalreg_obc = round(r(p),0.001)
	qui summ `var' if treatment == 0
	qui estadd scalar cm = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)

	qui summ temp2
	qui estadd scalar pvalri= r(mean)
	drop temp*
}


foreach var of varlist read_std math_std {

	noi di "`var' for Weak"
	*1) Without any controls
	*local var "time_study_out"

	qui ritest weakXtreat _b[weakXtreat], reps($reps): ///
	reg el_`var' treatment weakXtreat weak $indcontrols $schcontrols, vce(robust)
	matrix pvalues = r(p)
	gen temp2 = pvalues[1,1]

	qui eststo: bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reg el_`var' weak treatment weakXtreat $indcontrols $schcontrols, vce(robust)
	*eststo: reg `var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui test weakXtreat 
	qui estadd scalar pvalreg_obc = round(r(p),0.001)
	qui summ `var' if treatment == 0
*	qui estadd scalar cm = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)

	qui summ temp2
	qui estadd scalar pvalri= r(mean)
	drop temp*
}

# delimit ;
esttab using "$tables/Table5.tex", replace
keep(weak treatment weakXtreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Class X Exam Scores" "ASER Endline Scores", pattern(1 0 0 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitle("English" "Tamil" "Math" "Total" "Read" "Maths" )
stats(pvalreg_obc pvalri cm rsq N, labels("p-val (OLS)" "p-val (RI)" "\midrule Control Mean" "\midrule R2" "N") ///
fmt(2 2 2 2 0)) ///
collabels(, none) label booktabs nonotes
;
#delimit cr	





********************************************
* TABLE A4: MECHANISMS
********************************************
est clear
foreach var of varlist $mechanisms {

	noi di "Mechanisms `var' for Weak"
	*1) Without any controls
	*local var "time_study_out"

	qui ritest weakXtreat _b[weakXtreat], reps($reps): ///
	reg el_`var' treatment weakXtreat weak `var' $indcontrols $schcontrols, vce(robust)
	matrix pvalues = r(p)
	gen temp2 = pvalues[1,1]

	qui eststo: bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reg el_`var' `var' weak treatment weakXtreat $indcontrols $schcontrols, vce(robust)
	*eststo: reg `var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui test weakXtreat 
	qui estadd scalar pvalreg_obc = round(r(p),0.001)
	qui summ el_`var' if treatment == 0
	qui estadd scalar cm = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)

	qui summ temp2
	qui estadd scalar pvalri= r(mean)
	drop temp*
}
# delimit ;
esttab using "$tables/TableA4.tex", replace
keep(weak treatment weakXtreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Absenteeism" "Parent" "Tuition" "Self-Study", pattern(1 1 1 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitle("Days/Week" "Support" "At least 1 day" "Days/Week" "At least 1 day" "Days/Week")
stats(pvalreg_obc pvalri cm rsq N, labels("p-val (OLS)" "p-val (RI)" "\midrule Control Mean" "\midrule R2" "N") ///
fmt(2 2 2 2 0)) ///
collabels(, none) label booktabs nonotes
;
#delimit cr	

